﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.TEST;
using Entity.TEST;
using System.Text;
namespace Web.Controllers
{
    public class CategoryController : Web.Commons.BaseController
    {
        ICategoryRepository categoryRepository = null;
        public CategoryController()
        {
            categoryRepository = new CategoryRepository();
        }
        //
        // GET: /Category/

        public ActionResult Index(int? page)
        {
            GetCategory();
            VP = new Entity.VPredication();
            PP = new Entity.PagingParam(page ?? 1, base.PageSize);
            var model = categoryRepository.GetCategory(VP, PP);
            model.Last().IsSelected=true;
            return View(model);
        }

        #region 普通ASPX页面上的CURD操作
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            Category entity = new Category();
            TryUpdateModel(entity);
            if (entity.IsValid)
            {
                categoryRepository.Insert(entity);
                return RedirectToAction("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                 {
                     ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                 });
                return View();
            }
        }
        public ActionResult Detail(int? id)
        {
            return View(categoryRepository.Find(id.Value));
        }
        public ActionResult Edit(int? id)
        {
            return View(categoryRepository.Find(id.Value));
        }
        [HttpPost]
        public ActionResult Edit(FormCollection form)
        {
            Category entity = categoryRepository.Find(Convert.ToInt32(form["id"]));
            TryUpdateModel(entity);
            if (entity.IsValid)
            {
                categoryRepository.Update(entity);
                return RedirectToAction("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
                return View();
            }
        }
        public ActionResult Delete(int? id)
        {
            if (id.HasValue)
                categoryRepository.Delete(new Category { ID = id.Value});

            return RedirectToAction("Index");
        }
        #endregion

        #region 将CURD操作集成在分部视图上
        public ActionResult UCCreate()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UCCreate(FormCollection form)
        {
            Category entity = new Category();
            TryUpdateModel(entity);
            if (entity.IsValid)
            {
                categoryRepository.Insert(entity);
                return RedirectToAction("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
                return View();
            }
        }

        public ActionResult UCEdit(int? id)
        {
            ViewData["query"] = Request.UrlReferrer.Query;
            return View(categoryRepository.Find(id.Value));
        }
        [HttpPost]
        public ActionResult UCEdit(FormCollection form)
        {
            Category entity = categoryRepository.Find(Convert.ToInt32(form["id"]));
            TryUpdateModel(entity);
            if (entity.IsValid)
            {
                categoryRepository.Update(entity);
                //return RedirectToAction("Index", new { page = form["page"] });
                return Redirect("Index" + form["query"]);
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
                return View();
            }
        }
        #endregion

        #region 分类树结构
        /// <summary>
        /// 得到分类
        /// </summary>
        public void GetCategory()
        {
            Category Category = CategoryRepository.GetTree();
            StringBuilder html = new StringBuilder();
            html.Append("<ul id='containerul'>");
            this.GetSubCategory(html, Category, 0);
            html.Append("</ul>");
            ViewData["CategoryData"] = html.ToString();
        }

        /// <summary>
        /// 通过递归拼树形结构
        /// </summary>
        /// <param name="html">树形Html字符串</param>
        /// <param name="category">树形商品分类对象</param>
        private void GetSubCategory(StringBuilder html, Category category, int num)
        {
            num++;
            html.Append("<li id='" + category.ID + "' name='" + num.ToString() + "'>");
            html.Append("<a href='javascript:void(0)' id='" + category.Name + "'value='" + category.ID + "'>");
            html.Append("<ins>&nbsp;</ins>");
            html.Append(category.ID + "-" + category.Name + "(" + category.SortNumber + ")");
            html.Append("</a>");
            if (category.Sons != null && category.Sons.Count > 0)
            {
                html.Append("<ul>");
                foreach (var item in category.Sons)
                {
                    this.GetSubCategory(html, item, num);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }
        #endregion

    }
}
